# Chat related Pydantic schemas
from datetime import datetime
from typing import Optional, Dict, Any
from pydantic import BaseModel, Field
from enum import Enum


class SessionType(str, Enum):
    """会话类型枚举"""
    DISCUSSION = "discussion"
    ROLEPLAY = "roleplay"
    ANALYSIS = "analysis"


class SenderType(str, Enum):
    """发送者类型枚举"""
    USER = "user"
    AVATAR = "avatar"


class ContentType(str, Enum):
    """内容类型枚举"""
    TEXT = "text"
    AUDIO = "audio"
    IMAGE = "image"
    MIXED = "mixed"


# 消息相关schemas
class ChatMessageBase(BaseModel):
    content: str = Field(..., description="消息内容")
    content_type: ContentType = Field(ContentType.TEXT, description="内容类型")
    audio_url: Optional[str] = Field(None, description="音频文件URL")
    audio_duration: Optional[int] = Field(None, description="音频时长(秒)")
    emotion: Optional[str] = Field(None, description="情感标签")
    meta_data: Optional[Dict[str, Any]] = Field(None, description="元数据")
    parent_id: Optional[int] = Field(None, description="回复的消息ID")


class ChatMessageCreate(ChatMessageBase):
    session_id: int = Field(..., description="会话ID")
    sender_type: SenderType = Field(..., description="发送者类型")


class ChatMessageResponse(ChatMessageBase):
    id: int = Field(..., description="消息ID")
    session_id: int = Field(..., description="会话ID")
    sender_type: SenderType = Field(..., description="发送者类型")
    created_at: datetime = Field(..., description="创建时间")
    is_deleted: bool = Field(False, description="是否已删除")
    
    class Config:
        from_attributes = True


# 会话相关schemas
class ChatSessionBase(BaseModel):
    title: Optional[str] = Field(None, description="会话标题", max_length=200)
    topic: Optional[str] = Field(None, description="讨论话题", max_length=100)
    session_type: SessionType = Field(
        SessionType.DISCUSSION, description="会话类型"
    )
    context: Optional[str] = Field(None, description="会话上下文")
    roleplay_context: Optional[str] = Field(
        None, description="角色扮演背景设定"
    )


class ChatSessionCreate(ChatSessionBase):
    avatar_id: int = Field(..., description="数字人ID")
    novel_id: Optional[int] = Field(None, description="小说ID")
    character_id: Optional[int] = Field(None, description="扮演的角色ID")


class ChatSessionResponse(ChatSessionBase):
    id: int = Field(..., description="会话ID")
    user_id: int = Field(..., description="用户ID")
    avatar_id: int = Field(..., description="数字人ID")
    novel_id: Optional[int] = Field(None, description="小说ID")
    character_id: Optional[int] = Field(None, description="扮演的角色ID")
    message_count: int = Field(0, description="消息数量")
    last_message_at: datetime = Field(..., description="最后消息时间")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    is_active: bool = Field(True, description="是否激活")
    
    class Config:
        from_attributes = True


# API请求/响应schemas
class SendMessageRequest(BaseModel):
    character_id: int = Field(..., description="角色ID")
    character_name: str = Field(..., description="角色名称")
    novel_project: str = Field(..., description="小说项目")
    novel_title: str = Field(..., description="小说标题")
    message: str = Field(..., description="用户消息内容")
    session_id: Optional[int] = Field(None, description="会话ID，新会话时为空")


class ChatResponse(BaseModel):
    success: bool = Field(..., description="是否成功")
    session: Optional[ChatSessionResponse] = Field(
        None, description="会话信息"
    )
    character_message: Optional[ChatMessageResponse] = Field(
        None, description="角色回复消息"
    )
    error: Optional[str] = Field(None, description="错误信息")


# 向后兼容的别名
class MessageRequest(SendMessageRequest):
    pass


class MessageResponse(ChatResponse):
    pass 